﻿using System;
using System.Threading;
using System.Diagnostics;

namespace SimbaRGVMonitor.Controllers
{
    class MonitorTcpJob
    {
        public MonitorTcpJob(int interval, JsonData data)
            : this(interval, data, null)
        {

        }

        public MonitorTcpJob(int interval, JsonData data, Predicate<object> predicate)
        {
            this.data = data;
            this.interval = interval;
            this.predicate = predicate;
        }

        //====================================================================================================
        //以上是类型构造函数
        //----------------------------------------------------------------------------------------------------
        //以下是类型字段
        //====================================================================================================

        private int? currentInterval;
        private readonly int interval;
        private readonly JsonData data;
        private readonly Predicate<object> predicate;

        //====================================================================================================
        //以上是类型字段
        //----------------------------------------------------------------------------------------------------
        //以下是执行任务
        //====================================================================================================

        public void Excute(int tickInterval, TimeSpan timeout, MonitorTcpAgent tcpAgent, MonitorTcpDataBuffer buffer)
        {
            if(currentInterval == null)
            {
                SendData(timeout, tcpAgent, buffer);
            }
            else
            {
                currentInterval -= tickInterval;
                if(currentInterval <= 0)
                {
                    SendData(timeout, tcpAgent, buffer);
                }
            }
        }

        //====================================================================================================
        //以上是执行任务
        //----------------------------------------------------------------------------------------------------
        //以下是发送数据
        //====================================================================================================

        private void SendData(TimeSpan timeout, MonitorTcpAgent tcpAgent, MonitorTcpDataBuffer buffer)
        {
            tcpAgent.SendJsonData(data);
            currentInterval = interval;

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                while (true)
                {
                    object data;
                    if (predicate == null || buffer.TryTakeData(predicate, out data))
                    {
                        break;
                    }

                    if (tcpAgent.Connected == false)
                    {
                        return;
                    }

                    if (stopwatch.ElapsedMilliseconds > timeout.TotalMilliseconds)
                    {
                        return;
                    }

                    Thread.Sleep(10);
                }
            }
            finally
            {
                stopwatch.Stop();
            }
        }
    }
}
